package com.yuanlrc.base.controller.home;


import com.yuanlrc.base.annotion.ValidateEntity;
import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.constant.SessionConstant;
import com.yuanlrc.base.entity.common.HomeUser;
import com.yuanlrc.base.service.common.GoodsCategoryService;
import com.yuanlrc.base.service.common.HomeUserService;
import com.yuanlrc.base.util.SessionUtil;
import com.yuanlrc.base.util.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
/*
* 这是前端
* */

@RequestMapping("/home/index")
@Controller
public class IndexController
{
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @Autowired
    private HomeUserService homeUserService;

    @RequestMapping(value = "/index")
    public  String index(Model model){
        return "home/index/index";
    }
    @GetMapping(value = "/login")
    public  String login(Model model){
        return "home/index/login";
    }

    /**
     * 退出登录
     * @param
     * @return
     */
    @GetMapping(value = "/logout")
    public  String logout(){
        SessionUtil.set(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY,null);
        return "redirect:login";
    }
    /**
     * 检查账号是否存在
     */
    @PostMapping (value = "/home_user")
    @ResponseBody
    public Result<Boolean> homeUser(@RequestParam(name="userid",required = false)String userid){
        HomeUser homeuser = homeUserService.findByUserid(userid);
        return Result.success(homeuser==null);
    }

    @PostMapping (value = "/register")
    @ResponseBody
    public Result<Boolean> register(HomeUser homeUser){
        CodeMsg validate = ValidateEntityUtil.validate(homeUser);
        if(validate.getCode()!= CodeMsg.SUCCESS.getCode()){
            return  Result.error(validate);
        }
        if(homeUserService.findByUserid(homeUser.getUserid())!=null){
            return Result.error(CodeMsg.ADMIN_HOMEUSER_REGISTER_USERID_EXIST);
        }
        homeUser= homeUserService.save(homeUser);
        if(homeUser==null){
            return Result.error(CodeMsg.ADMIN_HOMEUSER_REGISTER_ERROR);
        }
        SessionUtil.set(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY,homeUser);
        return Result.success(true);
    }
    @PostMapping("/login")
    @ResponseBody
    public Result<Boolean> login(@RequestParam(name = "userid",required = false) String userid,
                                 @RequestParam(name = "password",required = false)String password){
        HomeUser homeUser = homeUserService.findByUserid(userid);
        /**
         * 验证账号是否正确
         */
        if(homeUser==null){
            return Result.error(CodeMsg.ADMIN_HOMEUSER_USER_NO_ERROR);
        }
        /**
         * 验证密码是否正确
         */
        if(!homeUser.getPassword().equals(password)){
            return Result.error(CodeMsg.ADMIN_HOMEUSER_PASSWORD_ERROR);
        }
        homeUser= homeUserService.save(homeUser);
        if(homeUser==null){
            return Result.error(CodeMsg.ADMIN_HOMEUSER_REGISTER_ERROR);
        }
        if(homeUser.getStatus()!=HomeUser.HOMEUSER_STATUS_ENABLE){
            return Result.error(CodeMsg.ADMIN_HOMEUSER_UNABLE);
        }
        SessionUtil.set(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY,homeUser);
        return Result.success(true);
    }

}
